package Classes.Managers // ActionScript file
{
	import Classes.GameUtils.GU_NewThingsInfo;
	import Classes.GameUtils.LoadImg;
	
	import UIComponents.FlatInfo;
	
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.external.ExternalInterface;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;

	/**
	 *配置表管理器
	 * @author 葛增文
	 *
	 */
	public class M_ConfigData extends Manager
	{
		private static var _instance:M_ConfigData; /*静态引用*/
		private var _xmls:Object; /*XML配置表存储器*/
		private var _csvs:Object; /*CSV配置表存储器*/
		private var _mainPath:String; /*主路径*/
		private var _needLoadAry:Array; /*需求载入数组*/
		private var _waiteTimer:Number;
		private var _commandArr:Array=new Array();

		private var _musicPath:String; //音乐路径
		
		private var _feedFunc:Function;

		private var _pullBack:Boolean;
		private var _hiredFriendNum:int;
		private var _isOpenQQVip:Boolean;
		private var _isOpenHz:Boolean;
		private var _isOpenSeeQb:Boolean;
		private var _qqVipLink:String;
		private var _qqYearVipLink:String;
		private var _addFansUrl:String;
		private var _exchangeItemId:Number;
		private var _imgUrl:String;
		private var _baseCartoonsUrl:String;
		private var _dayRewardItems:String;
		private var _uiPackageUrl:String;
		private var _cdn:String;
		private var _goFriendHomeLoadingUrl:String;
		private var _isOnLocalHost:int;
		
		private var _shopPicUrl:String;
		
		private var _toHomeItem:int;
		private var _addSpeedItem:int;
		private var _addSpeedTimer:int;
		private var _addLifeItem:int;
		
		private var _newGuideMission:String;
		
		private var _isOpenNewThing:Boolean;
		
		private var _isPlatformProviderFeed:Boolean;
		
		private var _fast_buy_shutiao:int;
		private var _bgpicUrl:String;
		
		private var _isOpenExchangMoney:Boolean;
		
		private var _missionShareNum:int;
		private var _maxVisitleNum:int;
		
		private var _isAddPlatformCommandAry:Array;

		public function M_ConfigData():void
		{
			_instance=this;
			this._mainPath=mainPath;
			this.init();
		}

		private function init():void
		{
			this._xmls=new Object;
			this._csvs=new Object;
		}

		override public function importData():void
		{
			this.cdn=this.main.mainXML.cdn;
			this._mainPath=this.cdn+this.main.mainXML.configDatasUrl;
			
			
			this.imgUrl=this.main.mainXML.img;
			this.baseCartoonsUrl=this.imgUrl+this.main.mainXML.baseCartoonsUrl;
			
			this.musicPath=this.cdn+this.main.mainXML.musicPath;
			this.uiPackageUrl=this.cdn+this.main.mainXML.uiPackageUrl;
			this.goFriendHomeLoadingUrl=this.cdn+this.main.mainXML.goFriendHomeLodingUrl;
			
			this.waiteTimer=Number(this.main.mainXML.waiteTimer);
			this.commandArr=String(this.main.mainXML.command).split(",");
			this.pullBack=int(this.main.mainXML.pullBack) == 1 ? true : false;
			this.hiredFriendNum=int(this.main.mainXML.hiredFriendNum);
			this.isOpenQQVip=int(this.main.mainXML.isOpenQQVip) == 1 ? true : false;
			this.isOpenHz=int(this.main.mainXML.isOpenHz) == 1 ? true : false;
			this.isOpenSeeQb=int(this.main.mainXML.isOpenSeeQb) == 1 ? true : false;
			this.qqVipLink=this.main.mainXML.qqVipLink;
			this.qqYearVipLink=this.main.mainXML.qqYearVipLink;
			this.exchangeItemId=this.main.mainXML.exchangeItemId;
			this.dayRewardItems=this.main.mainXML.dayRewardItems;
			this.isOnLocalHost=int(this.main.mainXML.isOnLocalHost);
			this.isOpenExchangMoney=int(this.main.mainXML.isOpenExchangMoney) == 1;
			
			this.addSpeedItem=this.main.mainXML.addSpeedItem;
			this.addSpeedTimer=this.main.mainXML.addSpeedTimer;
			this.toHomeItem=this.main.mainXML.toHomeItem;
			this.addLifeItem=this.main.mainXML.addLifeItem;
			this.newGuideMission=this.main.mainXML.newGuideMission;
			this.fast_buy_shutiao=this.main.mainXML.fast_buy_shutiao;
			this.bgpicUrl=this.imgUrl+this.main.mainXML.bgpicUrl;
			
			this.shopPicUrl=this.imgUrl+this.main.mainXML.shopPicUrl;
			this.maxVisitleNum=this.main.mainXML.maxVisitleNum;
			
			this.addFansUrl=this.main.mainXML.addFansUrl;
			
			this.missionShareNum=this.main.mainXML.missionShareNum;
			
			this.isAddPlatformCommandAry	=	String(this.main.mainXML.isAddPlatformCommand).split(",");
			
			
			this.isPlatformProviderFeed=int(this.main.mainXML.isPlatformProviderFeed)==1;
			
			this.isOpenNewThing=int(this.main.mainXML.someNewThings.@isOpen)==1
			if(this.isOpenNewThing)
			{
				GU_NewThingsInfo.instance.initNewThings(this.main.mainXML.someNewThings);
			}
			
			this.loadData(this.mainPath + this.main.mainXML.resourceDatasList);
		}

		/**
		 *载入XML
		 * @param path XML路径
		 *
		 */
		private function loadData(path:String):void
		{
			var loader:URLLoader=new URLLoader;
			var request:URLRequest=new URLRequest(path + ZombieTown.instance.pageParams);
			loader.dataFormat=URLLoaderDataFormat.TEXT;
			loader.addEventListener(Event.COMPLETE, loadOverHandler);
			loader.load(request);
		}

		/**
		 *载入完成操作
		 * @param e
		 *
		 */
		private function loadOverHandler(e:Event):void
		{
			(e.target as URLLoader).removeEventListener(Event.COMPLETE, loadOverHandler);
			var loadData:Object=(e.target as URLLoader).data;
			if (this.needLoadAry)
			{
				var nowNeed:Object=this.needLoadAry.shift();
				switch (nowNeed.type)
				{
					case "xml":
						this.xmls[nowNeed.dataName]=loadData;
						break;
					case "csv":
						this.csvs[nowNeed.dataName]=loadData;
						break;
				}
				this.needCheck();
			}
			else if (this.mainPath)
			{
				this._needLoadAry=new Array;
				var initXML:XML=XML(loadData);
				for (var i:int=0; i < (initXML.configData as XMLList).length(); i++)
				{
					var needConfig:XML=initXML.configData[i];
					this.needLoadAry.push({dataName: String(needConfig.dataName), path: String(needConfig.path), type: String(needConfig.type)});
				}
				this.needCheck();
			}
		}

		private function needCheck():void
		{
			if (this.needLoadAry.length)
			{
				this.loadData(this.mainPath + this.needLoadAry[0].path);
			}
			else
			{
				this.importOver();
			}
		}

		/**
		 *根据配置表名获得XML配置表
		 * @param str 配置表名
		 * @return XML配置表
		 *
		 */
		public function getXMLByName(str:String):XML
		{
			return XML(this.xmls[str]);
		}

		/**
		 *根据配置表名获得CSV配置表
		 * @param str 配置表名
		 * @return CSV配置表
		 *
		 */
		public function getCSVByName(str:String):String
		{
			return String(this.csvs[str]);
		}

		/**
		 *释放
		 *
		 */
		public function release():void
		{

		}

		/*getter & setter*/
		public static function get instance():M_ConfigData
		{
			return _instance;
		}
		
		/**
		 * 修改 窗口模式 
		 * @param mode
		 * 
		 */		
		public function changeParams(mode:String):void
		{
//			if (ExternalInterface.available)
//			{
//				ExternalInterface.call("changeParams", mode);
//			}
		}

		public function get xmls():Object
		{
			return _xmls;
		}

		public function get mainPath():String
		{
			return _mainPath;
		}

		public function get needLoadAry():Array
		{
			return _needLoadAry;
		}

		public function get csvs():Object
		{
			return _csvs;
		}

		/**
		 * 系统等待时间
		 */
		public function get waiteTimer():Number
		{
			return _waiteTimer;
		}

		/**
		 * @private
		 */
		public function set waiteTimer(value:Number):void
		{
			_waiteTimer=value;
		}

		/**
		 * 无需计时的命令数组
		 */
		public function get commandArr():Array
		{
			return _commandArr;
		}

		/**
		 * @private
		 */
		public function set commandArr(value:Array):void
		{
			_commandArr=value;
		}

		/**
		 * 是否开发黑名单功能
		 */
		public function get pullBack():Boolean
		{
			return _pullBack;
		}

		/**
		 * @private
		 */
		public function set pullBack(value:Boolean):void
		{
			_pullBack=value;
		}

		/**
		 * 好友雇佣上限值
		 */
		public function get hiredFriendNum():int
		{
			return _hiredFriendNum;
		}

		/**
		 * @private
		 */
		public function set hiredFriendNum(value:int):void
		{
			_hiredFriendNum=value;
		}

		/**
		 * 是否开放Q币购买
		 */
		public function get isOpenQQVip():Boolean
		{
			return _isOpenQQVip;
		}

		/**
		 * @private
		 */
		public function set isOpenQQVip(value:Boolean):void
		{
			_isOpenQQVip=value;
			FlatInfo.isOPenQQVip=value;
		}

		public function get qqVipLink():String
		{
			return _qqVipLink;
		}

		public function set qqVipLink(value:String):void
		{
			_qqVipLink=value;
			FlatInfo.vipLink=value;
		}

		/**
		 * 用来进行购买建筑的ID
		 */
		public function get exchangeItemId():Number
		{
			return _exchangeItemId;
		}

		/**
		 * @private
		 */
		public function set exchangeItemId(value:Number):void
		{
			_exchangeItemId=value;
		}

		public function get musicPath():String
		{
			return _musicPath;
		}

		public function set musicPath(value:String):void
		{
			_musicPath=value;
		}

		/**
		 * 每日登陆奖励
		 */
		public function get dayRewardItems():String
		{
			return _dayRewardItems;
		}

		/**
		 * @private
		 */
		public function set dayRewardItems(value:String):void
		{
			_dayRewardItems=value;
		}

		/**
		 * 图片根目录
		 */
		public function get imgUrl():String
		{
			return _imgUrl;
		}

		/**
		 * @private
		 */
		public function set imgUrl(value:String):void
		{
			_imgUrl=value;
		}

		/**
		 * 裸模路径 
		 */
		public function get baseCartoonsUrl():String
		{
			return _baseCartoonsUrl;
		}

		/**
		 * @private
		 */
		public function set baseCartoonsUrl(value:String):void
		{
			_baseCartoonsUrl = value;
		}

		/**
		 * 界面资源路径 
		 */
		public function get uiPackageUrl():String
		{
			return _uiPackageUrl;
		}

		/**
		 * @private
		 */
		public function set uiPackageUrl(value:String):void
		{
			_uiPackageUrl = value;
		}

		/**
		 * cdn 
		 */
		public function get cdn():String
		{
			return _cdn;
		}

		/**
		 * @private
		 */
		public function set cdn(value:String):void
		{
			_cdn = value;
		}

		/**
		 * 去好友家的loading 
		 */
		public function get goFriendHomeLoadingUrl():String
		{
			return _goFriendHomeLoadingUrl;
		}

		/**
		 * @private
		 */
		public function set goFriendHomeLoadingUrl(value:String):void
		{
			_goFriendHomeLoadingUrl = value;
		}

		/**
		 * 是否在本机   1 为 在本机  0为不在本机
		 */
		public function get isOnLocalHost():int
		{
			return _isOnLocalHost;
		}

		/**
		 * @private
		 */
		public function set isOnLocalHost(value:int):void
		{
			_isOnLocalHost = value;
			FlatInfo.isOnLocalHost=value;
		}

		/**
		 * 回城道具 
		 */
		public function get toHomeItem():int
		{
			return _toHomeItem;
		}

		/**
		 * @private
		 */
		public function set toHomeItem(value:int):void
		{
			_toHomeItem = value;
		}

		/**
		 * 加速物品 
		 */
		public function get addSpeedItem():int
		{
			return _addSpeedItem;
		}

		/**
		 * @private
		 */
		public function set addSpeedItem(value:int):void
		{
			_addSpeedItem = value;
		}

		/**
		 * 加速影响的时间 
		 */
		public function get addSpeedTimer():int
		{
			return _addSpeedTimer;
		}

		/**
		 * @private
		 */
		public function set addSpeedTimer(value:int):void
		{
			_addSpeedTimer = value;
		}

		/**
		 * 加血物品ID 
		 */
		public function get addLifeItem():int
		{
			return _addLifeItem;
		}

		/**
		 * @private
		 */
		public function set addLifeItem(value:int):void
		{
			_addLifeItem = value;
		}

		public function get newGuideMission():String
		{
			return _newGuideMission;
		}

		public function set newGuideMission(value:String):void
		{
			_newGuideMission = value;
		}

		/**
		 * 是否打开新鲜开关 true代表打开
		 */
		public function get isOpenNewThing():Boolean
		{
			return _isOpenNewThing;
		}

		/**
		 * @private
		 */
		public function set isOpenNewThing(value:Boolean):void
		{
			_isOpenNewThing = value;
		}

		/**
		 * 游戏平台是否自带新鲜事 true代表是
		 */
		public function get isPlatformProviderFeed():Boolean
		{
			return _isPlatformProviderFeed;
		}

		/**
		 * @private
		 */
		public function set isPlatformProviderFeed(value:Boolean):void
		{
			_isPlatformProviderFeed = value;
		}

		/**
		 * 新鲜是回调的方法 
		 */
		public function get feedFunc():Function
		{
			return _feedFunc;
		}

		/**
		 * @private
		 */
		public function set feedFunc(value:Function):void
		{
			_feedFunc = value;
		}

		/**
		 * 快速购买薯条ID 
		 */
		public function get fast_buy_shutiao():int
		{
			return _fast_buy_shutiao;
		}

		/**
		 * @private
		 */
		public function set fast_buy_shutiao(value:int):void
		{
			_fast_buy_shutiao = value;
		}

		/**
		 * 背景图片地址 
		 */
		public function get bgpicUrl():String
		{
			return _bgpicUrl;
		}

		/**
		 * @private
		 */
		public function set bgpicUrl(value:String):void
		{
			_bgpicUrl = value;
		}

		public function get qqYearVipLink():String
		{
			return _qqYearVipLink;
		}

		public function set qqYearVipLink(value:String):void
		{
			_qqYearVipLink = value;
			FlatInfo.qqYearVipLink=value;
		}

		/**
		 * 是否开发黄钻 
		 */
		public function get isOpenHz():Boolean
		{
			return _isOpenHz;
		}

		/**
		 * @private
		 */
		public function set isOpenHz(value:Boolean):void
		{
			_isOpenHz = value;
			FlatInfo.isOpenHz=value;
		}

		/**
		 * 是否开放查看Q币功能 
		 */
		public function get isOpenSeeQb():Boolean
		{
			return _isOpenSeeQb;
		}

		/**
		 * @private
		 */
		public function set isOpenSeeQb(value:Boolean):void
		{
			_isOpenSeeQb = value;
			FlatInfo.isOpenSeeQb=value;
			
		}

		/**
		 * 是否开启，人民币换成银币的功能  
		 */
		public function get isOpenExchangMoney():Boolean
		{
			return _isOpenExchangMoney;
		}

		/**
		 * @private
		 */
		public function set isOpenExchangMoney(value:Boolean):void
		{
			_isOpenExchangMoney = value;
			FlatInfo.isOpenExchangMoney=value;
		}

		/**
		 * 加入粉丝的地址 
		 */
		public function get addFansUrl():String
		{
			return _addFansUrl;
		}

		/**
		 * @private
		 */
		public function set addFansUrl(value:String):void
		{
			_addFansUrl = value;
		}

		/**
		 * 任务新鲜事分享次数 
		 */
		public function get missionShareNum():int
		{
			return _missionShareNum;
		}

		/**
		 * @private
		 */
		public function set missionShareNum(value:int):void
		{
			_missionShareNum = value;
		}

		/**
		 *  商店图片目录
		 */
		public function get shopPicUrl():String
		{
			return _shopPicUrl;
		}

		/**
		 * @private
		 */
		public function set shopPicUrl(value:String):void
		{
			_shopPicUrl = value;
		}

		/**
		 * 每天拜访的最大好友数目 
		 */
		public function get maxVisitleNum():int
		{
			return _maxVisitleNum;
		}

		/**
		 * @private
		 */
		public function set maxVisitleNum(value:int):void
		{
			_maxVisitleNum = value;
		}

		
		public function set isAddPlatformCommandAry(v:Array):void
		{
			this._isAddPlatformCommandAry	=	v;
		}
		
		public function get isAddPlatformCommandAry():Array
		{
			return this._isAddPlatformCommandAry;
		}

	}
}